<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2017/1/8
 * Time: 11:38
 */

namespace Home\Controller;


use Common\Model\CommodityModel;
use Common\Model\SiteModel;

class AccountsController extends HomeQController
{
    public function index(){
        $conf=[
            'js'=>['list','account','Areax','AreaData_min']
        ];
        $this->assign('conf',$conf);
        $confData=[
            'css'=>['account']
        ];
        $this->assign('confData',$confData);
        if(IS_POST){
            if(!isset($_POST['sid'])){
                $this->error('请选择要下单的商品');die;
            }
            $sids=I('post.sid');
            $data=[];
            foreach($sids as $k=>$sid){
                $data['order'][$sid]=$_SESSION['cart']['goods'][$sid];
                $data['order'][$sid]['pic']=(new CommodityModel())->where("gid={$_SESSION['cart']['goods'][$sid]['id']}")->getField('pic');
            }
            $data['total']=0;
            $data['total_rows']=0;
            foreach($data['order'] as $k=>$v){
                $data['total']=$data['total']+$v['total'];
                $data['total_rows']=$data['total_rows']+$v['num'];
            }
            //收货地址处理
            $site=(new SiteModel())->where("uid={$_SESSION['uid']}")->select();
//            $this->assign('site',$site);
//            dd($site);die;
            $_SESSION['order']=$data;
            $_SESSION['site']=$site;
            redirect(u('Home/Accounts/index'));die;

        }

        //显示模板
        $this->display();
    }
    //收货地址
    public function AjaxSite(){
        if(IS_AJAX) {
            $data = I('post.');
//        $data['dizhi']=$data['dizhi'] . $data['particular'];
            $data['uid'] = $_SESSION['uid'];
            (new SiteModel())->add($data);
            echo 1;
            exit;
        }else{
            echo 1;
        }
    }
    //异步接收下单的地址
    public function AjaxOrder(){
        $post=I('post.');
        echo json_encode(['valid'=>1,'message'=>$post]);die;
    }
}